//
//  WlisNavigationC.swift
//  Swift_missScarlett
//
//  Created by xmg on 16/8/13.
//  Copyright © 2016年 wlis. All rights reserved.
//

import UIKit

class WlisNavigationC: UINavigationController {

    override class func initialize(){
    
        let navigationBar = UINavigationBar.appearance();
        
        var attr = [String:AnyObject]();
        /** 设置字体 */
        attr[NSFontAttributeName] = UIFont.boldSystemFontOfSize(20);
        /** 设置文字 */
        navigationBar.titleTextAttributes = attr;
        /** 设置图片 */
        navigationBar.setBackgroundImage(UIImage(named: "navigationbarBackgroundWhite"), forBarMetrics: .Default);
    }
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        if self.childViewControllers.count > 0{
            /** 是否隐藏底部条 */
            viewController.hidesBottomBarWhenPushed = true;
            /** 创建按钮 */
            let backBtn = UIButton(type: .Custom);
            
            /** 设置按钮文字,字体颜色,选中颜色,设置图片 */
            backBtn.setTitle("返回", forState: .Normal);
            backBtn.setTitleColor(UIColor.blackColor(), forState: .Normal);
            backBtn.setTitleColor(UIColor.redColor(), forState: .Highlighted);
            backBtn.setImage(UIImage(named: "navigationButtonReturn"), forState: .Normal);
            backBtn.setImage(UIImage(named: "navigationButtonReturnClick"), forState: .Highlighted);
            /** 设置字体 */
            backBtn.sizeToFit();
            /** 设置点击事件 */
            backBtn.addTarget(self, action: "pushBtnClick", forControlEvents: .TouchUpInside);
            backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn);
        }
        /** 调用动画别忘记加载上此 */
        super.pushViewController(viewController, animated: animated);
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        
        /** 创建手势,调用系统写好的手势方法,当点击返回,或者手指滑动 */
       let pan = UIPanGestureRecognizer(target: self.interactivePopGestureRecognizer?.delegate, action: "handleNavigationTransition:")
        /** 添加手势 */
        self.view.addGestureRecognizer(pan);
        /** 设置代理 */
        pan.delegate = self;
        
        self.interactivePopGestureRecognizer?.enabled = false;
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
/** 实现手势的代理方法 */
extension WlisNavigationC :UIGestureRecognizerDelegate{

    /** 当有子控制器的时候才能用手势,否则不能用手势 */
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailByGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return self.childViewControllers.count > 1
    }
}

private extension WlisNavigationC {
    /** 执行返回 */
    @objc func pushBtnClick (){
        self.popViewControllerAnimated(true);
    }
}
